;
(function (window, angular) {
    'use strict';
    angular.module("Directive.Selector", [])

        .directive("roleSelector", ['$document', function ($document) {
            return {
                restrict: "EA",
                replace: "true",
                scope: {
                    selectedRole: "=?ngModel"
                },
                controller: 'RoleSelectorCtrl',
                templateUrl: "js/directive/tpl/roleSelector.html",
                link: function ($scope, element, attrs) {
                    $document.on('click', function () {
                        if (!!$scope.enablePopup) {
                            $scope.$apply(function () {
                                $scope.enablePopup = false;
                            });
                        }
                    });
                }
            }
        }])
        .controller("RoleSelectorCtrl", ["$scope", "$attrs", "ybHttp", "ybWidget", "$timeout", function ($scope, $attrs, ybHttp, ybWidget, $timeout) {
            $scope.enablePopup = false;
            $scope.upPosition = angular.isDefined($attrs.popupPosition) ? ($attrs.popupPosition == 'up') : false;
            $scope.dataList = [];
            var roleSelected = {};
            roleSelected = angular.copy($scope.selectedRole);

            $scope.toggleDropDown = function () {
                if (!!$scope.enablePopup) {
                    return;
                }
                roleSelected = angular.copy($scope.selectedRole);
                getDataList();
                $timeout(function () {
                    $scope.enablePopup = true;
                });
            };

            function setItemChecked() {
                if (!!roleSelected.list && roleSelected.list.length > 0) {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        $scope.dataList[i].isChecked = false;
                        for (var j = 0, lengj = roleSelected.list.length; j < lengj; j++) {
                            $scope.dataList[i].isChecked = false;
                            if ($scope.dataList[i].id == roleSelected.list[j]) {
                                $scope.dataList[i].isChecked = true;
                                break;
                            }
                        }
                    }
                }
            }

            function getDataList() {
                var service = "auth";
                var reqUrl = "/scepter/roles";
                var param = {};
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value, function (item) {
                        item.isChecked = false;
                    });
                    $scope.dataList = resp.value;
                    setItemChecked();
                });
            }

            $scope.ok = function () {
                var selectedRole = [];
                var selectRoleName = "";
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!!$scope.dataList[i].isChecked) {
                        selectedRole.push($scope.dataList[i].id);
                        if (selectRoleName === "") {
                            selectRoleName = $scope.dataList[i].name;
                        } else {
                            selectRoleName = selectRoleName + "," + $scope.dataList[i].name;
                        }
                    }
                }
                $scope.selectedRole = {
                    list: selectedRole,
                    name: selectRoleName
                };
                closePopup();
            };

            $scope.cancel = function () {
                closePopup();
            };

            function closePopup() {
                $scope.enablePopup = false;
            }
        }])

        .directive("departmentSelector", ['$document', function ($document) {
            return {
                restrict: "EA",
                replace: "true",
                scope: {
                    selectedObj: "=ngModel"
                },
                controller: 'DepartmentSelectorCtrl',
                templateUrl: "js/directive/tpl/departmentSelector.html",
                link: function ($scope, element, attrs) {
                    $document.on('click', function () {
                        if (!!$scope.enablePopup) {
                            $scope.$apply(function () {
                                $scope.enablePopup = false;
                            });
                        }
                    });
                }
            }
        }])
        .controller("DepartmentSelectorCtrl", ["$scope", "$attrs", "ybHttp", "ybWidget", "$timeout", function ($scope, $attrs, ybHttp, ybWidget, $timeout) {
            $scope.enablePopup = false;
            $scope.activeFlag = [true, false, false];
            $scope.firstClassList = [];
            $scope.secondClassList = [];
            $scope.thirdClassList = [];
            $scope.tabObj = [];
            $scope.upPosition = angular.isDefined($attrs.popupPosition) ? ($attrs.popupPosition == 'up') : false;

            function getDealerClassList(id) {
                var service = "auth";
                var reqUrl = "/department/list";
                var param = {};
                param.id = id;
                return ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code != 0) {
                        ybWidget.ShowMessage(resp);
                        return;
                    }
                    return resp.value;
                })
            }

            $scope.toggleDropDown = function () {
                if (!!$scope.enablePopup) {
                    return;
                }
                getDealerClassList("0").then(function (data) {
                    if (!!data) {
                        $scope.firstClassList = data;
                    }
                });
                $timeout(function () {
                    $scope.enablePopup = true;
                });
            };

            $scope.changeTab = function (tab) {
                for (var i = 0; i < $scope.activeFlag.length; i++) {
                    $scope.activeFlag[i] = false;
                }
                $scope.activeFlag[tab] = true;
            };

            $scope.selectClassItem = function (rank, item) {
                switch (rank) {
                    case 1:
                        $scope.tabObj = [];
                        $scope.tabObj[0] = item;
                        $scope.secondClassList = [];
                        $scope.thirdClassList = [];
                        getDealerClassList(item.id).then(function (data) {
                            if (!!data) {
                                $scope.secondClassList = data;
                                if ($scope.secondClassList.length > 0) {
                                    $scope.changeTab(1);
                                }
                            }
                        });
                        break;
                    case 2:
                        $scope.tabObj.length = 2;
                        $scope.tabObj[1] = item;
                        $scope.thirdClassList = [];
                        getDealerClassList(item.id).then(function (data) {
                            if (!!data) {
                                $scope.thirdClassList = data;
                                if ($scope.thirdClassList.length > 0) {
                                    $scope.changeTab(2);
                                }
                            }
                        });
                        break;
                    case 3:
                        $scope.tabObj[2] = item;
                        break;
                    default:
                        break;
                }
            };

            $scope.ok = function () {
                var lastIndex = $scope.tabObj.length - 1;
                if (lastIndex >= 0) {
                    $scope.selectedObj.departName = $scope.tabObj[lastIndex].name;
                    $scope.selectedObj.id = $scope.tabObj[lastIndex].id;
                }
                closePopup();
            };

            $scope.cancel = function () {
                closePopup();
            };

            function closePopup() {
                $scope.enablePopup = false;
            }

        }])

    ;
})(window, window.angular);



